<template>
  <div id="inspect">
    <el-row class="edit-title">
      <el-col :span="4">新增检查套餐</el-col>
      <el-col :span="3" :push="17" class="text-right">
        <el-button
          class="back"
          type="info"
          size="mini"
          plain
          icon="el-icon-arrow-left"
          @click="goBack"
        >返回</el-button>
      </el-col>
    </el-row>
    <el-form :model="info" :rules="rules" ref="info" label-width="120px" class="el_t_20">
      <el-form-item label="检查套餐名称" prop="name">
        <el-row>
          <el-col :span="8">
            <el-input v-model="info.name" placeholder="请输入检查名称"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="套餐内容：" prop="list">
        <template v-if="info.list.length > 0" v-for="(item,index) in info.list">
          <el-row :class="{'el_t_20':index > 0}">
            <el-col :span="8">
              <el-select v-model="item.radio" placeholder="检查1" class="el_long">
                <el-option label="检查1" :value="1"></el-option>
                <el-option label="检查2" :value="2"></el-option>
              </el-select>
            </el-col>
            <el-col :span="2" class="text-right">
              <el-button type="info" plain icon="el-icon-plus" class="tool-btn" @click="addLine"></el-button>
            </el-col>
            <el-col :span="2" class="text-right" v-if="info.list.length > 1">
              <el-button
                type="info"
                plain
                icon="el-icon-minus"
                class="tool-btn"
                @click="minusLine(index)"
              ></el-button>
            </el-col>
          </el-row>
        </template>
      </el-form-item>
    </el-form>
    <el-row class="footer-fixed">
      <el-col :span="24" class="text-right">
        <el-button type="primary" plain>取 消</el-button>
        <el-button type="primary" @click="submitInfo('info')">保 存</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  name: "inspect",
  data() {
    var checkList = (rule, value, callback) => {
      if (this.info.list.length <= 0) {
        return callback(new Error("检查内容不能为空！"));
      }
    };
    return {
      info: {
        name: "",
        list: [{ radio: 1 }, { radio: 2 }]
      },
      rules: {
        name: [
          { required: true, message: "检查名称不能为空", trigger: "blur" }
        ],
        list: [
          { validator: checkList, trigger: "blur" },
          { required: true, message: "检查内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    addLine() {
      this.info.list.push({ radio: 1 });
    },
    minusLine(index) {
      if (this.info.list.length === 1) {
        this.$message.warning("最少有意向检查内容！");
        return false;
      }
      this.info.list.splice(index, 1);
    },
    submitInfo(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("验证通过！");
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.go(-1);
    }
  }
};
</script>
<style scoped>
#inspect {
  width: 100%;
  height: calc(100vh - 108px);
  background: #ffffff;
  padding: 20px;
}
.edit-title {
  padding-bottom: 10px;
  border-bottom: 1px solid #e4e4e4;
  color: #006835;
  font-weight: 700;
}
#inspect .tool-btn {
  padding: 12px;
}
</style>


